package org.csu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.domain.Cart;
import org.csu.demo.domain.CartList;
import org.csu.demo.domain.CartVo;
import org.csu.demo.domain.Item;
import org.csu.demo.persistence.CartListMapper;
import org.csu.demo.persistence.ItemMapper;
import org.csu.demo.service.CartService;
import org.csu.demo.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Repository("cartService")
public class CartServiceImpl implements CartService {

    @Autowired
    private CartListMapper cartListMapper;

    @Autowired
    private CatalogService catalogService;


    //计算列表项的总价
    private BigDecimal calculate(Cart cart){
        return cart.getItem().getListPrice().multiply(BigDecimal.valueOf(cart.getCartList().getQuantity()));
    }

    //登录时获取所有的
    public CommonResponse<CartVo> findAllCarts(String username) {
        QueryWrapper<CartList> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<CartList> cartLists = cartListMapper.selectList(wrapper);
        String itemId = null;
        List<Cart> carts = new ArrayList<>();
        BigDecimal price = BigDecimal.ZERO;
        BigDecimal total = BigDecimal.ZERO;
        CartVo cartVo = new CartVo();
        for(CartList cartList : cartLists) {
            Cart cart = new Cart();
            itemId = cartList.getItemId();
            CommonResponse<Item> itemCommonResponse = catalogService.findItemById(itemId);
            cart.setCartList(cartList);
            cart.setItem(itemCommonResponse.getData());
            price = calculate(cart);
            total = total.add(price);
            cart.setItemPrice(price);
            carts.add(cart);
        }
        cartVo.setCarts(carts);
        cartVo.setTotalPrice(total);
        return CommonResponse.createForSuccess(cartVo);
    }

    public CommonResponse<CartVo> updateCart(String itemId,String username,int quantity) {
        boolean result = cartListMapper.updateCartQuantity(username,quantity,itemId);
        if(result) {
            return CommonResponse.createForSuccess();
        }else{
            return CommonResponse.createForError();
        }
    }

    public CommonResponse<CartVo> addCart(String itemId,String username) {
        CartList cartList = new CartList();
        cartList.setItemId(itemId);
        cartList.setUsername(username);
        //默认有货
        cartList.setInStock(true);
        cartList.setQuantity(1);
        boolean result = cartListMapper.insert(cartList)>0;
        if(result) {
            return findAllCarts(username);
        }else{
            return CommonResponse.createForError();
        }
    }

    public boolean emptyCart(String username){
        QueryWrapper<CartList> cartListWrapper = new QueryWrapper<>();
        cartListWrapper.eq("username",username);
        return cartListMapper.delete(cartListWrapper)>0;
    }

    public CommonResponse<CartVo> addCart(String itemId,String username,int quantity) {
        CartList cartList = new CartList();
        cartList.setItemId(itemId);
        cartList.setUsername(username);
        //默认有货
        cartList.setInStock(true);
        cartList.setQuantity(quantity);
        boolean result = cartListMapper.insert(cartList)>0;
        if(result) {
            return findAllCarts(username);
        }else{
            return CommonResponse.createForError();
        }
    }

    public CommonResponse<BigDecimal> deleteCart(String username ,String itemId) {
        boolean result = cartListMapper.deleteCartList(username,itemId);
        BigDecimal total = BigDecimal.ZERO;
        QueryWrapper<CartList> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<CartList> cartLists = cartListMapper.selectList(wrapper);
        for (CartList cartList : cartLists) {
            Cart cart = new Cart();
            itemId = cartList.getItemId();
            CommonResponse<Item> itemCommonResponse = catalogService.findItemById(itemId);
            cart.setCartList(cartList);
            cart.setItem(itemCommonResponse.getData());
            BigDecimal price = calculate(cart); // 计算单个 Cart 的价格
            total = total.add(price); // 累加到 total
        }
        if(result) {
            return CommonResponse.createForSuccess(total);
        }else{
            return CommonResponse.createForError();
        }
    }
}
